package com.sunlong.jiangwei.web;


import com.sunlong.jiangwei.entity.JwSpu;
import com.sunlong.jiangwei.entity.result.Result;
import com.sunlong.jiangwei.service.IJwSpuService;
import com.sunlong.jiangwei.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孙龙
 * @since 2021-02-19
 */
@RestController
@RequestMapping("/jiangwei/jw-spu")
public class JwSpuController {

    @Autowired
    private IJwSpuService jwSpuService;

    /**
     * 利用es查询
     * @param map
     * @return
     */
    @PostMapping("/es/list")
    public Result<List<JwSpu>> esList(@RequestBody Map<String, Object> map){
        Map<String, Object> resultMap = jwSpuService.esList(map);
        if (null != resultMap && resultMap.containsKey("existError")){
            return ResultUtil.error((String) resultMap.get("errorMsg"));
        }
        return ResultUtil.data((List<JwSpu>) resultMap.get("data"));
    }

    @GetMapping("/list")
    public Result<List<JwSpu>> list(){
        List<JwSpu> list = jwSpuService.list();
        return ResultUtil.data(list);
    }
    @PostMapping("/list")
    public Result<List<JwSpu>> listSpu(@RequestBody Map<String, Object> map){
        List<JwSpu> spuList = jwSpuService.listSpu(map);
        return ResultUtil.data(spuList);

    }

    @PostMapping("/add")
    public Result<Void> add(@RequestBody Map<String, Object> map){
        Map<String,String> errorMap = jwSpuService.add(map);
        if (errorMap.containsKey("existError")){
            return ResultUtil.error(errorMap.get("errorMsg"));
        }
        return ResultUtil.success();
    }

    @PostMapping("/detail")
    public Result<JwSpu> detail(@RequestBody Map<String, Object> map){
        String id = (String) map.get("id");
        return ResultUtil.data(jwSpuService.detail((long) Integer.parseInt(id)));
    }

}
